<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelCurrency extends JModel
{
	/**
	 * currency data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * currency total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	/**
	 * Currency id
	 *
	 * @var int
	 */
	var $_id = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;

		$limit		= $mainframe->getUserStateFromRequest( $option.'.currency.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.currency.limitstart', 'limitstart', 0, 'int' );
		
		$limitstart = ( $limit != 0 ? (floor($limitstart / $limit) * $limit) : 0 );
		
		$id = JRequest::getVar('id');
		$this->setId((int)$id);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	/**
	 * Method to set the currency id
	 *
	 * @access	public
	 * @param	int	currency ID number
	 */
	function setId($id) {
		// Set new currency ID and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 * Method to get currency data
	 *
	 * @access public
	 * @return array
	 */
	function getData() {

		// Lets load the currency if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			//echo $query;
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}

		return $this->_data;
	}
	
	/**
	* Method to get Currency data for one item
	*
	* @access public
	* @return array
	*/
	function getItem() {
		// Lets load the Business if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQueryItem();
			$this->_db->setQuery( $query );
			//echo $query;
			$this->_data = $this->_db->loadObject();

			if(!is_object($this->_data)) {
				$this->_data =  & JTable::getInstance('ownservice_currency', '');
				$this->_data->published = 1;
			}
		}

		return $this->_data;
	}

	/**
	 * Method to get the total number of currency
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal() {
		// Lets load the currency if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the currency
	 *
	 * @access public
	 * @return object
	 */
	function getPagination() {

		// Lets load the currency if it doesn't already exist
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new FMCPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}

	/**
	 * Method to build the query for the currency
	 *
	 * @access private
	 * @return integer
	 * @since 1.0
	*/
	function _buildQuery()
	{
		$user =& JFactory::getUser();
		$aid = $user->get('aid', 0);

		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();

		//Query to retrieve all currency that belong under the web links section and that are published.
		$query = ' SELECT c.* '
				.' FROM #__ownservice_currencies AS c'
				.$where
				.$orderby;

		return $query;
	}
	
	/**
	 * Method to build the query for the item currency
	 *
	 * @access private
	 * @return integer
	 * @since 1.0
	 */
	function _buildQueryItem() {

		$query = 'SELECT * '
					.' FROM #__ownservice_currencies '
					.' WHERE id='.$this->_id
					;
					
		return $query;
	}

	/**
	 * Method to build the orderby clause of the query for the currency
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentOrderBy(){
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest($option.'.currency.filter_order', 'filter_order', 'c.name', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.currency.filter_order_Dir', 'filter_order_Dir', 'ASC', 'cmd');

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;

		return $orderby;
	}

	/**
	 * Method to build the where clause of the query for the currency
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentWhere()
	{
		global $mainframe, $option;

		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
				
		$where		= array();

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		return $where;

	}
	
	/**
	 * Method to store the currency
	 *
	 * @access public
	 * @return boolean True on success
	 * @since 1.0
	*/
	function store($data)
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$row = & JTable::getInstance('ownservice_currency', '');

		// bind it to the table
		if(!$row->bind($data)) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		// sanitise id field
		$row->id = (int) $row->id;

		// Make sure the data is valid
		if(!$row->check()) {
			$this->setError($row->getError() );
			return false;
		}

		// Store it in the db
		if (!$row->store()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}
		
		$this->_data = & $row;

		return true;
	}
	
	/**
	 * Method to check if we can remove a currency
	 * return false if there are items associated
	 *
	 * @access	public
	 * @return	boolean	True on success
	 * @since	1.5
	 */
	function candelete($id)
	{
		$query = 'SELECT COUNT( id )'
			. ' FROM #__ownservice_currencies '
			. ' WHERE id = '. (int) $id.' AND iscore=1 '
			;
		$this->_db->setQuery( $query );
		$count1 = $this->_db->loadResult();
		
		if ($count1 > 0) {
			return false;
		}
		
		$query = 'SELECT COUNT( id )'
			. ' FROM #__ownservice_exchangerates '
			. ' WHERE currency_id = '. (int) $id
			;
		$this->_db->setQuery( $query );
		$count2 = $this->_db->loadResult();
		
		if ($count2 > 0) {
			return false;
		}
		
		return true;
	}

	/**
	 * Method to remove a currency
	 *
	 * @access	public
	 * @return	boolean	True on success
	 * @since	1.0
	 */
	function delete($id)
	{
		$query = 'DELETE FROM #__ownservice_currencies'
				. ' WHERE id ='.$id
				;
		$this->_db->setQuery( $query );
		if(!$this->_db->query()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		
		return true;
	}
}